package com.yungu.swift.api.route.v2.common;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.api.base.gateway.CurrentUser;
import com.yungu.swift.api.utils.FileUploadUtil;
import com.yungu.swift.api.utils.RandomUtil;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.api.ApiParam;
import com.yungu.swift.common.model.dto.CommonErrorMessageDto;
import com.yungu.swift.common.model.param.ApiErrorMsgParam;
import com.yungu.swift.common.model.param.ApiSmsParam;
import com.yungu.swift.common.service.CommonErrorMessageService;
import com.yungu.swift.common.service.CommonSmsService;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.constants.MagicConstant;
import com.yungu.swift.utils.DateUtils;
import com.yungu.swift.utils.ParamUtil;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Date;
import java.util.Optional;

import static com.yungu.swift.api.base.ApiErrorEnum.PARAM_ERROR;

/**
 * @author : cuixiuyin
 * @date : 2019/11/22
 */
@RestController
@RequestMapping("/api/v2")
@Api(value = "【通用】【工具】通用工具接口V2", tags = "V2-通用工具接口")
public class CommonToolApi {

    @Reference
    private CommonSmsService commonSmsService;
    @Reference
    private CommonErrorMessageService commonErrorMessageService;

    @PostMapping(value = {"/common/identifyCode/send"})
    @ApiOperation(value = "通用发送验证码")
    public ResponseData<Boolean> sendIdentifyCode(@RequestBody ApiSmsParam apiSmsParam) {
        String decryptMobile = RandomUtil.randomDecrypt(apiSmsParam.getMobile());
        if (ParamUtil.isIllegal(decryptMobile)) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return commonSmsService.sendAliIdentify(decryptMobile, apiSmsParam.getAppid());
    }

    @PostMapping(value = {"/common/message/send"})
    @ApiOperation(value = "通用发送短信")
    public ResponseData<Boolean> sendMessgae(@RequestBody ApiSmsParam apiSmsParam) {
        String decryptMobile = RandomUtil.randomDecrypt(apiSmsParam.getMobile());
        if (ParamUtil.isIllegal(decryptMobile, apiSmsParam.getContent())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return commonSmsService.sendCommon(decryptMobile, apiSmsParam.getContent(), apiSmsParam.getAppid());
    }


    @PostMapping(value = {"/upload/image"})
    @ApiOperation(value = "上传图片接口")
    public ResponseData<String> uploadImage(@RequestPart MultipartFile file,
                                            @RequestHeader(required = false) String appid,
                                            @RequestParam(required = false) String mobile,
                                            @CurrentUser ApiParam apiParam) {
        String imageType = StringUtils.substringAfter(file.getOriginalFilename(), ".");
        if (!imageType.contains(MagicConstant.PICTURE_SUFFIX_JPG) && !imageType.contains(MagicConstant.PICTURE_SUFFIX_PNG)
                && !imageType.contains(MagicConstant.PICTURE_SUFFIX_JPEG) && !imageType.contains(MagicConstant.PICTURE_SUFFIX_BMP)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "图片格式不正确，请重新选择！");
        }
        return FileUploadUtil.uploadImage(file, Optional.ofNullable(apiParam.getAppid()).orElse(appid),
                Optional.ofNullable(apiParam.getMobile()).orElse(mobile), imageType);
    }

    @PostMapping(value = {"/upload/file"})
    @ApiOperation(value = "上传文件接口")
    public ResponseData<String> uploadFile(@RequestPart MultipartFile file,
                                           @RequestParam(required = false) String mobile,
                                           @RequestHeader(required = false) String appid,
                                           @CurrentUser ApiParam apiParam) {
        return FileUploadUtil.uploadFile(file, Optional.ofNullable(apiParam.getAppid()).orElse(appid),
                Optional.ofNullable(apiParam.getMobile()).orElse(mobile));
    }

    @PostMapping(value = {"/common/uploadErrorMsg"})
    @ApiOperation(value = "上传错误日志", notes = "上传错误日志")
    public ResponseData<Boolean> uploadErrorMsg(@Valid @RequestBody ApiErrorMsgParam errorMsgParam) {
        CommonErrorMessageDto commonErrorMessageDto = new CommonErrorMessageDto();
        try {
            commonErrorMessageDto.setErrorTime(DateUtils.parse(errorMsgParam.getErrorTime(), "yyyy-MM-dd HH:mm:ss"));
        } catch (Exception e) {
            commonErrorMessageDto.setErrorTime(DateUtils.parse(errorMsgParam.getErrorTime(), "yyyy-MM-dd"));
        }
        commonErrorMessageDto.setUuid(StringUtils.buildUUID());
        commonErrorMessageDto.setCreateOn(new Date());
        commonErrorMessageDto.setCreateBy(errorMsgParam.getUserUuid());
        commonErrorMessageDto.setUploadFrom(errorMsgParam.getRid() == CommonConstant.PASSENGER ?
                CommonErrorMessageDto.UPLOAD_FROM_PASSENGER : CommonErrorMessageDto.UPLOAD_FROM_DRIVER);
        commonErrorMessageDto.setErrorModule(errorMsgParam.getErrorModule());
        commonErrorMessageDto.setErrorType(errorMsgParam.getErrorType());
        commonErrorMessageDto.setErrorMsg(errorMsgParam.getErrorMsg());
        commonErrorMessageDto.setTerminalId(errorMsgParam.getTerminalId());
        commonErrorMessageDto.setTerminalVersion(errorMsgParam.getTerminalVersion());
        return commonErrorMessageService.add(commonErrorMessageDto);
    }
}
